import _ from "underscore";
import {h} from "vue";

export function isMoblie() {
    let userAgentInfo = navigator.userAgent;
    if (userAgentInfo.indexOf("Windows") < 0 || userAgentInfo.indexOf("Mac OS") < 0) {
        return false;
    } else {
        return true;
    }
}

export function isvalidPhone(str) {
    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
    return reg.test(str);
}

export function isValidLoginName(str) {
    const reg = /^[0-9a-zA-Z_]{1,}$/;
    return reg.test(str);
}

export function isValidEmail(str) {
    const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    return reg.test(str);
}

export function isvalidlng(str) {
    const reg = /^[\-\+]?(0(\.\d{1,10})?|([1-9](\d)?)(\.\d{1,10})?|1[0-7]\d{1}(\.\d{1,10})?|180\.0{1,10})$/;
    return reg.test(str);
}

export function isvalidlat(str) {
    const reg = /^[\-\+]?((0|([1-8]\d?))(\.\d{1,10})?|90(\.0{1,10})?)$/;
    return reg.test(str);
}

export function getPointsCenter(path) {
    let lngLen = 0, latLen = 0, lngTotal = 0, latTotal = 0, lng = 0, lat = 0;
    _.forEach(path, function (cood) {
        lngLen++;
        latLen++;
        lngTotal += cood[0];
        latTotal += cood[1];
    });
    lng = lngTotal / lngLen;
    lat = latTotal / latLen;
    return [lng, lat];
}

export function timeFormate(timeStamp) {
    let year = new Date(timeStamp).getFullYear();
    let month = new Date(timeStamp).getMonth() + 1 < 10 ? "0" + (new Date(timeStamp).getMonth() + 1) : new Date(timeStamp).getMonth() + 1;
    let date = new Date(timeStamp).getDate() < 10 ? "0" + new Date(timeStamp).getDate() : new Date(timeStamp).getDate();
    let hh = new Date(timeStamp).getHours() < 10 ? "0" + new Date(timeStamp).getHours() : new Date(timeStamp).getHours();
    let mm = new Date(timeStamp).getMinutes() < 10 ? "0" + new Date(timeStamp).getMinutes() : new Date(timeStamp).getMinutes();
    let ss = new Date(timeStamp).getSeconds() < 10 ? "0" + new Date(timeStamp).getSeconds() : new Date(timeStamp).getSeconds();
    var day = new Date(timeStamp).getDay(); //当前系统时间中的星期值
    var weeks = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    var week = weeks[day]; //根据星期值，从数组中获取对应的星期字符串
    /*  this.times.data = year + "-" + month + "-" + date;
      this.times.time = hh + ":" + mm;
      this.times.week = week;*/
    let data = {year, month, date, hh, mm, week, ss};
    return data;
}

export function renderHeader({column}) {
    let realWidth = 0;
    let span = document.createElement('span');

    span.innerText = column.label;
    document.body.appendChild(span);

    realWidth = span.getBoundingClientRect().width;
    column.minWidth = realWidth + 40; // 可能还有边距/边框等值，需要根据实际情况加上

    document.body.removeChild(span);
    return h('span', column.label);
}

export function setEchartsData(component) {
    console.log(component.showColumn);
    _.forEach(component.showChartsData.data, function (item) {
        item.type = component.showColumn[item.nameRow]
    });
    //配置参数
    setTimeout(function () {
        let showLength = 0;
        for (let item in component.showColumn) {
            if (component.showColumn[item]) {
                showLength++;
            }
        }
        component.$refs.DailyDosageStatisicsBar.setData(component.showChartsData, showLength);
    }, 100)

}
